package cn.rtplay.esp32camcar.toolUtils;

import android.app.ActivityManager;
import android.content.Context;

import java.util.List;
import java.util.Objects;

/**
 * The type Tools.
 */
public class Tools {
    /**
     * Exit app.
     *
     * @param context the context
     */
    public static void exitApp(Context context){
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.AppTask> appTaskList = Objects.requireNonNull(activityManager).getAppTasks();
        for (ActivityManager.AppTask appTask : appTaskList) {
            appTask.finishAndRemoveTask();
        }
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

    /**
     * Gets key string count.
     *
     * @param str the str
     * @param key the key
     * @return the key string count
     */
    public static int getKeyStringCount(String str, String key) {
        if ((str == null) || (key == null)) {
            return -1;
        }
        int count = 0;
        int start = 0;
        while (str.indexOf(key, start) >= 0 && start < str.length()) {
            count++;
            start = str.indexOf(key, start) + key.length();
        }
        return count;
    }
}
